package btreegui;

import java.awt.Color;
import java.awt.Component;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

public class TreeCellRenderer extends DefaultTreeCellRenderer {

    private Color searchColor = Color.red;
    private BTreeGUI frame;
    private String regexWithPointEtc = "[|]";
    private String whitespace = " ";

    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
        Component c = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
        try {
            if (!"".equals(frame.getDrawTreePanel().getSearchValue()) && value.toString().contains(frame.getDrawTreePanel().getSearchValue())) {    //grob Filter
                String clearedText = value.toString().replaceAll(regexWithPointEtc, whitespace);
                String[] split = clearedText.split(whitespace);
                for (String i : split) {
                    if (Integer.parseInt(i) == Integer.parseInt(frame.getDrawTreePanel().getSearchValue())) {   //fein Filter
                        c.setForeground(searchColor);
                    }
                }
            }
        } catch (Exception ex) {
        }
        return c;
    }

    public void setFrame(BTreeGUI frame) {
        this.frame = frame;
    }
}
